﻿// IpConDlg.cpp : implementation file
//

#include "stdafx.h"
#include "IpCon.h"
#include "IpConDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
														//}}AFX_VIRTUAL

														// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIpConDlg dialog

CWinThread *pThread;
BOOL g_Stop = TRUE;
UINT Vote(LPVOID lParam)
{
	CIpConDlg *dlg = (CIpConDlg*)lParam;
	//结束链接，重新拨号
	dlg->HangUp();
	dlg->DialUp();
	return 0;
}
CIpConDlg* CIpConDlg::pThis;
CIpConDlg::CIpConDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIpConDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIpConDlg)
	// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_strAdsl = _T("");
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CIpConDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIpConDlg)
	DDX_Control(pDX, IDC_TXT_CON_INFO, m_cConInfo);
	DDX_Control(pDX, IDC_LIST1, m_cConName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIpConDlg, CDialog)
	//{{AFX_MSG_MAP(CIpConDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIpConDlg message handlers

BOOL CIpConDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

									// TODO: Add extra initialization here
	EnumConn();
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CIpConDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CIpConDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM)dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIpConDlg::OnQueryDragIcon()
{
	return (HCURSOR)m_hIcon;
}

void CIpConDlg::EnumConn()//枚举系统里面的当前所有连接
{
	RASENTRYNAME Entries[15];
	DWORD cb, cEntries, dwRet;
	Entries[0].dwSize = sizeof(RASENTRYNAME);
	cb = sizeof(Entries);
	cEntries = 0;
	//重要就是这个函数 ，其中第3个参数是返回连接名称数组，第5个参数是返回目前连接数
	dwRet = ::RasEnumEntriesA(NULL, NULL, &Entries[0], &cb, &cEntries);  //如果函数成功，则返回0

	if (dwRet == 0)
	{
		m_cConName.ResetContent();
		for (UINT i = 0; i<cEntries; i++)
			m_cConName.AddString(Entries[i].szEntryName);

		m_cConName.SetCurSel(0);
	}
}

bool CIpConDlg::DialUp()
{
	// 重新拨号
	if (hRasConn)
	{
		//断开连接
		RasHangUp(hRasConn);
		hRasConn = NULL;
	}

	RASDIALPARAMS rdParams;
	DWORD dwRet;
	BOOL f_get_password;

	memset(&rdParams, 0, sizeof(rdParams));
	rdParams.dwSize = sizeof(RASDIALPARAMS);
	strcpy(rdParams.szEntryName, m_strAdsl);
	//获取连接参数（用户名，密码）根据szEntryName连接名
	if (RasGetEntryDialParams(NULL, &rdParams, &f_get_password) != 0)
	{
		SetDlgItemText(IDC_STATIC_NO_CON, "拨号失败，获取ADSL用户名失败！");
		return FALSE;
	}
	if (f_get_password == false)
	{
		SetDlgItemText(IDC_STATIC_NO_CON, "拨号失败，获取ADSL密码失败！");
		return FALSE;
	}
	//开始拨号
	//参数3传入拨号信息
	//RasDialFunc用来处理连接信息
	if ((dwRet = RasDial(NULL, NULL, &rdParams, 0L, RasDialFunc, &hRasConn)) != 0)
	{
		SetDlgItemText(IDC_STATIC_NO_CON, "拨号失败！请检查MODEM连接是否正常！");
		return FALSE;
	}
	return TRUE;

}

void CIpConDlg::OnOK()
{

	CString strBtn;
	GetDlgItem(IDOK)->GetWindowText(strBtn);
	UpdateData();
	if (strBtn == "开始")
	{
		//控制开关
		g_Stop = TRUE;
		m_cConName.GetText(m_cConName.GetCurSel(), m_strAdsl);
		//拨号
		DialUp();

		GetDlgItem(IDOK)->SetWindowText("停止");
	}
	else
	{
		//控制开关
		g_Stop = FALSE;

		//断号
		HangUp();
		GetDlgItem(IDOK)->SetWindowText("开始");
	}
}

void CIpConDlg::HangUp()
{
	DWORD   dwConnections;
	RASCONN   rasconn;
	DWORD   dwSize;
	DWORD   dwSuccess;

	SetDlgItemText(IDC_STATIC_NO_CON, "正在断开连接！");
	rasconn.dwSize = sizeof(RASCONN);
	dwSize = rasconn.dwSize;
	dwSuccess = RasEnumConnections(&rasconn, &dwSize, &dwConnections);
	if (dwSuccess == 0)
	{
		if (dwConnections)
		{
			//断掉连接句柄就可以了
			RasHangUp(rasconn.hrasconn);
			Sleep(3000);  //MSDN Sleep(3000);
			SetDlgItemText(IDC_STATIC_NO_CON, "连接已经断开！");
		}
	}

}



//回调函数
void CALLBACK CIpConDlg::RasDialFunc(UINT unMsg, RASCONNSTATE rasstate, DWORD dwError)
{

	switch (rasstate)
	{
	case RASCS_OpenPort:
		CIpConDlg::pThis->m_cConInfo.ResetContent();
		CIpConDlg::pThis->m_cConInfo.AddString(_T("打开端口……"));
		break;
	case RASCS_PortOpened:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("端口已打开."));
		break;
	case RASCS_ConnectDevice:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("连接设备……"));
		break;
	case RASCS_DeviceConnected:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("设备已连接."));
		break;
	case RASCS_Authenticate:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("验证用户及密码"));
		break;
	case RASCS_Authenticated:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("通过"));
		break;
	case RASCS_Connected:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("已连接"));

		pThread = AfxBeginThread(Vote, (LPVOID)CIpConDlg::pThis);
		pThread->m_bAutoDelete = TRUE;//线程为自动删除

		break;
	case RASCS_Disconnected:
		CIpConDlg::pThis->m_cConInfo.AddString(_T("连接已断开"));

		break;
	default:
		return;
	}
	return;
}
